package com.shine.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * JDBC工具类
 */
public class JDBCUtils {
    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
    private static DruidDataSource dataSource = null;
    private static final Properties PROPERTIES = new Properties();


    // 保证驱动只加载一次
    static {
        try {
            // 配置文件加载到Properties
            Class.forName("com.mysql.jdbc.Driver");
            PROPERTIES.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));

            // 创建连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(PROPERTIES);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接的方法
     * @return
     * @throws IOException
     * @throws SQLException
     */
    public static Connection getConnection() throws IOException, SQLException {
        // 从本地线程对象中获取连接对象
        Connection conn = THREAD_LOCAL.get();

        // 判断获取的对象是否是null,如果是null意味着还没有存入，先获取、存入，以后再获取就行啦
        if (conn == null){
            conn = dataSource.getConnection();
            THREAD_LOCAL.set(conn);
        }

        return conn;
    }

    /**
     * 关闭连接的方法
     * @param resultSet 结果集
     * @param stmt      执行SQL的对象
     * @param conn      连接对象
     */
    public static void closeConn(ResultSet resultSet, Statement stmt,Connection conn){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 开启事务
     */
    public static void begin(){
        Connection conn = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 提交事务
     */
    public static void commit(){
        Connection conn = null;
        try {
            conn = getConnection();
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 回滚事务
     */
    public static void rollback(){
        Connection conn = null;
        try {
            conn = getConnection();
            conn.rollback();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取连接池对象
     * @return
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
}